package com.guigu.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.base.entity.TransportTrips;
import com.guigu.base.entity.TransportTripsTruckDriver;
import com.guigu.base.entity.dto.TransportTripsDto;
import com.guigu.base.entity.dto.TransportTripsTruckDriverDto;
import com.guigu.base.service.TransportTripsService;
import com.guigu.base.service.TransportTripsTruckDriverService;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 车次信息表 前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/goods/pd-transport-trips")
@Api(tags = "车次信息管理")
public class TransportTripsController {
    @Autowired
    private TransportTripsService transportTripsService;
    @Autowired
    private TransportTripsTruckDriverService transportTripsTruckDriverService;

    /**
     * 添加车次
     * truckDriver
     *
     * @param dto 车次信息
     * @return 车次信息
     */
    @PostMapping("")
    @ApiOperation("添加车次")
    public R<TransportTripsDto> save(@RequestBody TransportTripsDto dto) {
        TransportTrips pdTransportTrips = new TransportTrips();
        BeanUtils.copyProperties(dto, pdTransportTrips);
        pdTransportTrips = transportTripsService.saveTransportTrips(pdTransportTrips);
        BeanUtils.copyProperties(pdTransportTrips, dto);
        return new R<>("2000", dto);
    }

    /**
     * 根据id获取车次详情
     *
     * @param id 车次id
     * @return 车次信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取车次详情")
    public R<TransportTripsDto> fineById(@PathVariable(name = "id") String id) {
        TransportTrips pdTransportTrips = transportTripsService.getById(id);
        TransportTripsDto dto = new TransportTripsDto();
        if (pdTransportTrips != null) {
            BeanUtils.copyProperties(pdTransportTrips, dto);
        } else {
            dto.setId(id);
        }
        return new R<>("2000", dto);
    }

    /**
     * 获取车次列表
     *
     * @param transportLineId 线路id
     * @param ids             车次id列表
     * @return 车次列表
     */
    @GetMapping("")
    @ApiOperation("获取车次列表")
    public R<List<TransportTripsDto>> findAll(@RequestParam(name = "transportLineId", required = false) String transportLineId, @RequestParam(name = "ids", required = false) List<String> ids) {
        return new R<>("2000", transportTripsService.findAll(transportLineId, ids).stream().map(pdTransportTrips -> {
            TransportTripsDto dto = new TransportTripsDto();
            BeanUtils.copyProperties(pdTransportTrips, dto);
            return dto;
        }).collect(Collectors.toList()));
    }

    /**
     * 更新车次信息
     *
     * @param id  车次id
     * @param dto 车次信息
     * @return 车次信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新车次信息")
    public R<TransportTripsDto> update(@PathVariable(name = "id") String id, @RequestBody TransportTripsDto dto) {
        dto.setId(id);
        TransportTrips pdTransportTrips = new TransportTrips();
        BeanUtils.copyProperties(dto, pdTransportTrips);
        transportTripsService.updateById(pdTransportTrips);
        return new R<>("2000", dto);
    }

    /**
     * 删除车次信息
     *
     * @param id 车次信息
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    @ApiOperation("删除车次信息")
    public R disable(@PathVariable(name = "id") String id) {
        transportTripsService.disable(id);
        return new R(ResultCode.SUCCESS_CODE, "删除车次信息成功!", true);
    }

    /**
     * 批量保存车次与车辆和司机关联关系
     *
     * @param dtoList 车次与车辆和司机关联关系
     * @return 返回信息
     */
    @PostMapping("{id}/truckDriver")
    @ApiOperation("批量保存车次与车辆和司机关联关系")
    public R batchSaveTruckDriver(@PathVariable("id") String transportTripsId, @RequestBody List<TransportTripsTruckDriverDto> dtoList) {
        transportTripsTruckDriverService.batchSave(transportTripsId, dtoList.stream().map(dto -> {
            dto.setTransportTripsId(transportTripsId);
            TransportTripsTruckDriver truckTransportTripsTruckDriver = new TransportTripsTruckDriver();
            BeanUtils.copyProperties(dto, truckTransportTripsTruckDriver);
            return truckTransportTripsTruckDriver;
        }).collect(Collectors.toList()));
        return new R(ResultCode.SUCCESS_CODE, "批量保存车次车辆信息成功!");
    }

    /**
     * 获取车次与车辆和司机关联关系列表
     *
     * @param transportTripsId 车次id
     * @param truckId          车辆id
     * @param userId           司机id
     * @return 车次与车辆和司机关联关系列表
     */
    @GetMapping("/truckDriver")
    @ApiOperation("获取车次与车辆和司机关联关系列表")
    public R<List<TransportTripsTruckDriverDto>> findAllTruckDriverTransportTrips(@RequestParam(name = "transportTripsId", required = false) String transportTripsId, @RequestParam(name = "truckId", required = false) String truckId, @RequestParam(name = "userId", required = false) String userId) {
        return new R<>("2000", transportTripsTruckDriverService.findAll(transportTripsId, truckId, userId).stream().map(pdTransportTripsTruck -> {
            TransportTripsTruckDriverDto dto = new TransportTripsTruckDriverDto();
            BeanUtils.copyProperties(pdTransportTripsTruck, dto);
            return dto;
        }).collect(Collectors.toList()));
    }

    /**
     * 根据线路Id来获取对应的车次集合数据
     *
     * @param lineId
     * @return
     */
    @ApiOperation("根据线路Id来获取对应的车次集合数据")
    @GetMapping("/findByLineID/{lineId}")
    public R<List<TransportTrips>> findByLineID(@PathVariable String lineId) {
        QueryWrapper<TransportTrips> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("transport_line_id", lineId);
        List<TransportTrips> list = transportTripsService.list(queryWrapper);
        return new R<>(ResultCode.SUCCESS_CODE, list);
    }
}

